// pages/mine/personalInformation/personalInformation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    deliveryAddress: '', // 收货地址
    avatar_name:''    //头像地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  /**
   * 获取用户信息
   */
  getUserInfo:function(){
    var appInstance = getApp()
    wx.showLoading({
      title:'加载中...',
      mask:true
    })
    appInstance.getAppUserInfo(true).then((res)=>{
      console.log('用户信息---------',res)
      wx.hideLoading()
      res.type = wx.filter.placetype(res.type)
      res.verify = wx.filter.authenType(res.verify)
      this.setData({
        userInfo: res,
        avatar_name: wx.envConfig.filePath + '/' + res.avatar_name
      })
      wx.stopPullDownRefresh();
    },()=>{
      wx.hideLoading()
    })
  },
  updateAvatarInfo (id, avatar_name) { // 更新用户头像信息
    wx.request({
      url: wx.envConfig.host + '/user/updateInfo?params='+ JSON.stringify({avatar: id}) + '&cookiez=' + wx.getStorageSync("cookiez"),
      method: 'POST',
      success: (res) => {
        console.log(res);
        wx.showToast({
          title: "头像已更改"
        });
        // this.setData({
        //   avatar_name: wx.envConfig.filePath + '/' + avatar_name
        // });
        this.getUserInfo()//获取最新的用户信息
        getApp().getAppUserInfo(true);
      }
    })
  },
  fileUpload (type) { // 文件上传
    var _this = this
    wx.chooseImage({
      count: 1,
      sourceType: [type],
      success:  (res) => {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths', tempFilePaths);
        /*
         * TODO 修改上传相关的方式路径.............................................
         */
        wx.showLoading({
          title: '加载中...',
          mask: true
        })
        wx.uploadFile({
          url: wx.envConfig.filePath + 'file/uploadFile',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            fileTemp: JSON.stringify({belongTo:"1"}),
            metadata: JSON.stringify([
              {name:"author",value:_this.data.userInfo.userName},   //当前登录人的用户名(不是昵称)
              {name:"where",value:"miniProgram"},     //来自哪个端，端名自己定
              {name:"type",value:"img"},          //自己大概估一个类型
              {name:"remark",value:"头像更新"}  //文件的业务逻辑(干嘛用的，大概的就行)
            ])
          },
          success:  (res) => {
            console.log('res', res);
            try {
              res.data = JSON.parse(res.data)
            } catch (e) {

            }
            wx.hideLoading()
            getApp().fileBinding(res.data.datas).then(ress => {
              this.updateAvatarInfo(ress.data.datas.id, res.data.datas.path)
            })
          },
          fail:()=>{
            wx.hideLoading()
          }
        });
      }
    })
  },
  selectUploadWays () { // 选择上传头像方式
    wx.showActionSheet({
      itemList: ['拍照上传','从相册中选择'],
      success: (res) => {
        if (res.tapIndex == 0) {
          this.fileUpload('camera');
        }
        if (res.tapIndex == 1) {
          this.fileUpload('album');
        }
      }
    })
  },
  imgOnError () {
    this.setData({
      avatar_name: "../../../resource/icon/Me/Unselect@2x.png"
    });
  },
  /**
   * 进入收货地址
   */
  gointoAddress: function () {
    wx.navigateTo({
      url: './deliveryAddress/deliveryAddress',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 点击认证
   */
  goAuth: function () {
    if (this.data.userInfo.verify == '待认证'){
      wx.navigateTo({
        url: './authenticationData/authenticationData',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else{
      wx.navigateTo({
        url: './authenticated/authenticated',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    
  }
})